// pages/pay/insensibility/insensibility.js
const app = getApp()
const request = require("../../../utils/request.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    screenHeight: app.globalData.screenHeight,
    skypeMoneyIndex: 0,
    skypeRuleList: [],
    skypeMoney: 0,
    totalPay: 0,
    agreementVip: true,
    verifyLoginDialog: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let token = wx.getStorageSync('lmtc_token');
    if (token) {
      this.getUserInfo()
      this.getSkypeRule()
    } else {
      this.setData({
        verifyLoginDialog: true
      })
    }

  },
  verifyLogin() {
    this.getUserInfo()
    this.getSkypeRule()
  },
  /**
 * 获取充值金额
 */
  getSkypeRule() {
    let that = this
    request.http('/promotion/recharge-rule/getRechargeRule', 'POST')
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            skypeRuleList: data.data,
            skypeMoney: data.data[that.data.skypeMoneyIndex],
            totalPay: data.data[that.data.skypeMoneyIndex],
          })
        }
      })
  },
  /**
   * 会员充值
   */
  selectSkype(e) {
    let index = e.currentTarget.dataset.index
    let money = e.currentTarget.dataset.money
    this.setData({
      skypeMoneyIndex: index,
      skypeMoney: money,
      totalPay: money
    })
  },
  /**
   * 自定义金额获取焦点
   */
  skypeInputFocus(e) {
    let money = e.detail.value
    this.setData({
      skypeMoneyIndex: -1,
      totalPay: money,
      skypeMoney: money
    })
  },
  /**
   * 获取自定义金额
   */
  getSkypeInput(e) {
    let money = e.detail.value
    if (money > 9999) {
      wx.showToast({
        title: '请输入9999以下数字',
        icon: 'none'
      })
      this.setData({
        skypeMoney: money.substring(0, 4)
      })
      return
    }
    this.setData({
      skypeMoney: money,
      totalPay: money
    })
  },
  /**
 * 用户协议弹窗
 */
  showAgreement(e) {
    let type = e.currentTarget.dataset.type
    this.setData({
      agreementShow: true,
      agreementType: type
    })
  },
  /**
   * 关闭用户协议弹窗
   */
  showAgreementClose() {
    this.setData({
      agreementShow: false
    })
  },
  /**
   * 同意协议按钮事件
   */
  showAgreementCloseEvent(e, a) {
    let agreement = e.detail
    this.setData({
      agreementVip: agreement
    })
  },
  /**
 * 获取用户信息
 */
  getUserInfo() {
    let that = this;
    let url = '/customer/info/getCustomerInfo'
    request.http(url, 'GET')
      .then((res) => {
        if (res.data.code == 200) {
          let user = res.data.data;
          if (user != null && user != undefined) {
            let phone = user.mobilePhone;
            that.setData({
              mobilePhone: phone
            })
          }
        } else {
          Toast.fail('获取数据失败');
        }
      })
  },
  /**
   * 获取手机号
   * @param {*} e 
   */
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let encryptedData = e.detail.encryptedData;
      let iv = e.detail.iv;
      wx.login({
        success(res) {
          let url = '/customer/info/bindMobilePhone'
          let params = {
            code: res.code,
            encryptedData: encryptedData,
            iv: iv
          }
          request.http(url, 'GET', params)
            .then(res => {
              if (res.data.code == 200) {
                let phone = res.data.data
                Toast.success('绑定成功');
                that.setData({
                  mobilePhone: phone
                })
                that.checkPop()
              }
            })
        }
      })
    }
  },
  /**
 * 跳转支付页
 */
  toPayment() {
    if (!this.data.agreementVip) {
      wx.showToast({
        title: '请先阅读并同意会员服务协议',
        icon: 'none'
      })
      return
    }
    let data = {}
    if (this.data.skypeMoney <= 0) {
      wx.showToast({
        title: '请输入您要充值的金额',
        icon: 'none'
      })
      return
    }
    if (this.data.skypeMoney > 9999) {
      wx.showToast({
        title: '请输入9999以下数字',
        icon: 'none'
      })
      return
    }
    data = {
      paidAmount: this.data.skypeMoney,
      type: 'skype'
    }
    wx.navigateTo({
      url: '/pages/confirmOrder/confirmOrder?payData=' + JSON.stringify(data),
    })
  }
})